package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.pojo.Device;
import com.zzyl.nursing.domain.pojo.DeviceData;
import com.zzyl.nursing.job.IotMsgBody;
import com.zzyl.nursing.job.IotMsgHeader;
import com.zzyl.nursing.job.IotMsgNotifyData;
import com.zzyl.nursing.job.IotMsgService;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.nursing.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * deviceDateService业务层处理
 * 
 * @author alexis
 * @date 2025-05-30
 */
@Service
@Slf4j
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService
{
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询deviceDate
     * 
     * @param id deviceDate主键
     * @return deviceDate
     */
    @Override
    public DeviceData selectDeviceDataById(Long id)
    {
        return deviceDataMapper.selectById(id);
    }

    /**
     * 查询deviceDate列表
     * 
     * @param deviceData deviceDate
     * @return deviceDate
     */
    @Override
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData)
    {
        return deviceDataMapper.selectDeviceDataList(deviceData);
    }

    /**
     * 新增deviceDate
     * 
     * @param deviceData deviceDate
     * @return 结果
     */
    @Override
    public int insertDeviceData(DeviceData deviceData)
    {
        return deviceDataMapper.insert(deviceData);
    }

    /**
     * 修改deviceDate
     * 
     * @param deviceData deviceDate
     * @return 结果
     */
    @Override
    public int updateDeviceData(DeviceData deviceData)
    {
        return deviceDataMapper.updateById(deviceData);
    }

    /**
     * 批量删除deviceDate
     * 
     * @param ids 需要删除的deviceDate主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataByIds(Long[] ids)
    {
        return deviceDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除deviceDate信息
     * 
     * @param id deviceDate主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataById(Long id)
    {
        return deviceDataMapper.deleteById(id);
    }
@Autowired
private StringRedisTemplate stringRedisTemplate;


    @Override
    public void BatchSave(IotMsgNotifyData notifyData) {
        log.info("asdasdasdasdasdasdasdasdsad");
        IotMsgHeader header = notifyData.getHeader();
        String deviceId = header.getDeviceId();
        Device device = deviceService.lambdaQuery().eq(Device::getIotId, deviceId).one();
        if (ObjectUtil.isEmpty(device)) {
            return;
        }
        IotMsgBody body = notifyData.getBody();
        if (ObjectUtil.isEmpty(body)) {
            return;
        }
        List<IotMsgService> services = body.getServices();
        services.forEach(s->{
            ArrayList<DeviceData> deviceDateList = new ArrayList<>();
            Map<String, Object> properties = s.getProperties();

            LocalDateTime localDateTime = LocalDateTimeUtil.parse(s.getEventTime(), "yyyyMMdd'T'HHmmss'Z'");
            LocalDateTime eventTime = localDateTime.atZone(ZoneId.from(ZoneOffset.UTC)).withZoneSameInstant(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
            properties.forEach((k,v)->{
                DeviceData deviceData = BeanUtil.toBean(device, DeviceData.class, CopyOptions.create().setIgnoreProperties("id"));
                deviceData.setAccessLocation(device.getBindingLocation());
                deviceData.setFunctionId(k);
                deviceData.setDataValue(v.toString());
                deviceData.setAlarmTime(eventTime);
                deviceDateList.add(deviceData);
            });
                stringRedisTemplate.opsForHash().put(CacheConstants.IOT_DEVICE_LAST_DATA,deviceId, JSONUtil.toJsonStr(deviceDateList));
               this.saveBatch(deviceDateList);
        });
    }
}
